package com.yhf.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yhf.dto.DeveloperDto;
import com.yhf.mapper.DeveloperMapper;
import com.yhf.model.Contractor;
import com.yhf.model.Developer;
import com.yhf.model.Project;
import com.yhf.service.DeveloperService;
import com.yhf.service.PService;
import com.yhf.utils.PageResult;
import com.yhf.utils.QueryPageBean;
import com.yhf.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("developer")
public class DeveloperController {
    @Autowired
    DeveloperService developerService;
    @Autowired
    PService pService;
    @RequestMapping("all")
    public PageResult all(@RequestBody QueryPageBean queryPageBean){
        QueryWrapper<Developer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1);

        Page<Developer> page = new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
//        Page<Project> page1 = pService.page(page);
        Page<Developer> page1 = developerService.page(page, queryWrapper);
        PageResult result = new PageResult();
        result.setRows(page1.getRecords());
        result.setTotal(page1.getTotal());
        return result;

    }
    @RequestMapping("allproject")
    public Result allproject(){
        Result result = new Result();
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1);
        List<Project> list = pService.list(queryWrapper);
        if (list != null) {
            result.setData(list);
            result.setFlag(true);
            result.setMessage("success");
            return result;
        }else {
            return Result.fail();
        }

    }
    @RequestMapping("add")
    public Result addDeveloper(String dname,int [] pids){
        System.out.println(dname);
        System.out.println(Arrays.toString(pids));
        Developer developer = new Developer();
        developer.setDname(dname);
        developer.setStatus(1);
        boolean save = developerService.save(developer);
        Project project = new Project();
        for (int pid : pids) {
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("pid",pid);
            project.setDid(developer.getDid());
            pService.update(project, queryWrapper);
        }

        Result result  = new Result();
        if (save){
            result.setMessage("success");
            result.setFlag(true);
        }else {
            result.setMessage("fail");
        }
        return result;
    }

    /**
     * 根据departmentId查department并通过departmentdto封装包含的project信息
     * @param did departmentId
     * @return Result
     */
    @GetMapping("findpjanddnameById")
    public Result findBydnId(@RequestParam String did){
     QueryWrapper<Developer> queryWrapper = new QueryWrapper<>();
     QueryWrapper<Project> queryWrapper1 = new QueryWrapper<>();
     queryWrapper.eq("did",did);
     queryWrapper1.eq("did",did);
        Developer one = developerService.getOne(queryWrapper);
        List<Project> list = pService.list(queryWrapper1);
        DeveloperDto developerDto = new DeveloperDto();
        int[] arr = new int[list.size()];
        for (int i = 0;i<list.size();i++){
            arr[i] = list.get(i).getPid();
        }
        developerDto.setPids(arr);
        developerDto.setDid(one.getDid());
        developerDto.setDname(one.getDname());
        Result result = new Result();
        result.setData(developerDto);
        result.setFlag(true);
        result.setMessage("success");
        return result;
    }

    /**
     * 修改开发商
     * @param developerDto 部门增强
     * @return Result
     */
    @PostMapping("edit")
    public Result edit(@RequestBody DeveloperDto developerDto){
        Developer contractor = new Developer();
        contractor.setDname(developerDto.getDname());
        contractor.setUpdatetime(new Date());
        QueryWrapper<Developer> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("did",developerDto.getDid());
        //修改开发商名称和修改时间
        developerService.update(contractor,queryWrapper3);

        System.out.println(developerDto);
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("did",developerDto.getDid());
        Project project = new Project();
        project.setDid("");
        pService.update(project,queryWrapper);
        Project project1 = new Project();
        project1.setDid(developerDto.getDid());


        for (int i = 0; i < developerDto.getPids().length; i++) {
            QueryWrapper<Project> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("pid",developerDto.getPids()[i]);
            System.out.println(developerDto.getPids()[i]);
            System.out.println(project1);
            pService.update(project1,queryWrapper1);
        }
        return Result.success();
    }

    /**
     * 通过did将开发商状态改为0
     * @param did 开发商id
     * @return Result
     */
    @GetMapping("delete")
    public Result delete(@RequestParam String did){
        QueryWrapper<Developer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("did",did);
        Developer developer = new Developer();
        developer.setStatus(0);
        developerService.update(developer,queryWrapper);
        QueryWrapper<Project> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("did",did);
        Project project = new Project();
        project.setDid("");
        pService.update(project,queryWrapper1);
        return Result.success();

    }
}
